<template>
  <el-select v-model="currentId" placeholder="请选择货币" @change="change"
             style="width: 100%">
    <el-option
      v-for="item in datas"
      :key="item.id"
      :label="item.symbol+' '+item.name"
      :value="item.id">
    </el-option>
  </el-select>
</template>
<script>
  import AdminService from 'src/service/admin'
  import emitter from 'src/components/emitter'

  export default {
    name: 'HCurrencySelect',
    mixins: [emitter],
    data() {
      return {
        datas: []
      }
    },
    computed: {
      currentId: {
        get: function () {
          return this.value
        },
        set: function (data) {
          this.$emit('input', data)
        }
      }
    },
    props: {
      value: {
        type: Number
      }
    },
    methods: {
      getDatas: function () {
        const me = this
        AdminService.currency().then(datas => {
          if (datas != null) {
            me.datas = datas.datas
          }
        })
      },
      change: function (item) {
        if (item > 0) {
          const datas = this.datas.filter(data => {
            return data.id === item
          })
          this.$emit('change', datas[0])
          this.dispatch('ElFormItem', 'el.form.change', [item])
        }
      }
    },
    beforeMount: function () {
      this.getDatas()
    }
  }
</script>
